package cn.hxzy.company.controller;

import cn.hxzy.company.entity.Member;
import cn.hxzy.company.entity.News;
import cn.hxzy.company.entity.Work;
import cn.hxzy.company.entity.vo.PageBean;
import cn.hxzy.company.service.MemberService;
import cn.hxzy.company.service.NewsService;
import cn.hxzy.company.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author little black
 * 用户查看页面
 */
@Controller
public class UserIndexController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private WorkService workService;

    //页面显示
    @GetMapping("/")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("index");
        List<Work> works = workService.queryAll();
        modelAndView.addObject("works", works);
        List<Member> members = memberService.queryAll();
        modelAndView.addObject("members", members);
        List<News> news = newsService.queryAll();
        modelAndView.addObject("news", news);
        return modelAndView;
    }

    //新闻动态
    @GetMapping("/newsList")
    public ModelAndView newsList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size) {
        ModelAndView modelAndView = new ModelAndView("newsList");
        PageBean pageBean = new PageBean<News>();
        pageBean.setPage(page);
        pageBean.setSize(size);
        newsService.queryPage(pageBean);
        modelAndView.addObject("newsPage", pageBean);
        return modelAndView;

    }

    //作品展示
    @GetMapping("/workList")
    public ModelAndView workList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size) {
        ModelAndView modelAndView = new ModelAndView("workList");
        PageBean pageBean = new PageBean<Work>();
        pageBean.setPage(page);
        pageBean.setSize(size);
        workService.queryPage(pageBean);
        modelAndView.addObject("workPage", pageBean);
        return modelAndView;
    }

    //公司员工
    @GetMapping("/memberList")
    public ModelAndView memberList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size) {
        ModelAndView modelAndView = new ModelAndView("memberList");
        PageBean pageBean = new PageBean<Member>();
        pageBean.setPage(page);
        pageBean.setSize(size);
        memberService.queryPage(pageBean);
        modelAndView.addObject("memberPage", pageBean);
        return modelAndView;
    }

    //作品详解
    @GetMapping("/showWork/{id}")
    public ModelAndView showWork(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("showWork");
        Work work = workService.queryById(id);
        modelAndView.addObject("work", work);
        return modelAndView;
    }

    //新闻详情
    @GetMapping("/showNews/{id}")
    public ModelAndView showNews(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("showNews");
        modelAndView.addObject("news", newsService.queryById(id));
        return modelAndView;
    }
}
